package behavioral.mediator;

public class ArticlesDialogBox {
    private final ListBox articlesListBox = new ListBox();
    private final TextBox titleTextBox = new TextBox();
    private final Button saveButton = new Button();

    public ArticlesDialogBox() {
        articlesListBox.addObserver(this::articleSelected);

        Observer observer = this::titleTextChanged;

        titleTextBox.addObserver(this::titleTextChanged);

        // 匿名类不是同一个对象，因此不能移除
        titleTextBox.removeObserver(this::titleTextChanged);
    }

    public void simulateUserInteraction() {
        articlesListBox.setSelection("Article 1");
        titleTextBox.setContent("");
        titleTextBox.setContent("Article 2");

        System.out.println("TextBox：" + titleTextBox.getContent());
        System.out.println("Button：" + saveButton.isEnabled());
    }


    private void articleSelected() {
        titleTextBox.setContent(articlesListBox.getSelection());
        saveButton.setEnabled(true);
    }

    private void titleTextChanged() {
        var content = titleTextBox.getContent();
        var isEmpty = (content == null) || content.isEmpty();
        saveButton.setEnabled(!isEmpty);
    }
}
